package lpca.view 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import kioschool.basic.view.View;	
	import lpca.controllers.LpcaController;
	import lpca.model.LpcaModel;
	import lpca.utils.Point;
	import lpca.utils.Cell;
	import lpca.utils.ContainerViewCells;
	import flash.events.MouseEvent;
	import lpca.controllers.SecondaryController;
	
	/**
	 * ...
	 * @author 
	 */
	public class ViewMaze extends Sprite implements View
	{
		private var model: LpcaModel;
		
		public function update(): void
		{
			for (var i: int = 0; i < this.numChildren; i++)	
			{
				(this.getChildAt(i) as ViewCell).update();							
			}
		}		
				
		public function ViewMaze(model: LpcaModel, controller: LpcaController) 
		{
			this.model = model;				
			//создание лабиринта			
			var vCell: ViewCell;									
			var typeView: int;
			var iterateY: int;
			var iterateX: int;									
			for ( iterateY = 1; iterateY <= model.getSizeY(); iterateY++)
			{
				for (iterateX = 1; iterateX <= model.getSizeX(); iterateX++)
				{
					
					switch(model.getCell(new Point(iterateX, iterateY)).value)
					{
						case Cell.CELL_EMPTY:  typeView = ContainerViewCells.TYPE_EMPTY; break;						
						case Cell.CELL_WALL:   typeView = ContainerViewCells.TYPE_WALL; break;
						case Cell.CELL_START:  typeView = ContainerViewCells.TYPE_START; break;
						case Cell.CELL_FINISH: typeView = ContainerViewCells.TYPE_FINISH; break;
						default: typeView = -1; 
					}
					vCell = new ViewCell(new Point(iterateX, iterateY),  model, controller.handler, 
					                     ContainerViewCells.getCells(typeView), 
										 ContainerViewCells.getCells(ContainerViewCells.TYPE_MASK)); 
					if (vCell != null)       
                       {
                            vCell.x = (iterateX - 1) * ContainerViewCells.width;
                            vCell.y = (iterateY - 1) * ContainerViewCells.height;																					
							var cell: Cell = model.getCell(new Point(iterateX, iterateY));						
							this.addChild(vCell);			
							cell.view = vCell;	
							cell.visibleValueWave = false;
							model.setCell(new Point(iterateX, iterateY), cell);									                            												
                       }
                    else
                       trace("null");					
				}					
			}			
			this.model.clearStackChange();			
		}
				
	}

}